#!/sbin/openrc-run

supervisor=supervise-daemon

name="hitch"
extra_commands="checkconfig"
extra_started_commands="reload"
description="A scalable TLS proxy"
description_checkconfig="Verify configuration file"
description_reload="Reload configuration"

: ${cfgfile:=${HITCH_CONFIGFILE:-"/etc/hitch/hitch.conf"}}

command="/usr/sbin/hitch"
command_args="--config=$cfgfile"

required_files="$cfgfile"


depend() {
	need net
	before logger
}

configtest() {
	ebegin "Checking $SVCNAME configuration"
	checkconfig
	eend $?
}

checkconfig() {
	hitch -t --config=$cfgfile >/dev/null 2>&1
	ret=$?
	if [ $ret -ne 0 ]; then
		eerror "$SVCNAME has detected an error:"
		hitch -t --config=$cfgfile
	fi

	return $ret
}

start_pre() {
	checkconfig || return 1
}

stop_pre() {
	if [ "$RC_CMD" = "restart" ]; then
		checkconfig || return 1
	fi
}

reload() {
	checkconfig || return 1
	ebegin "Reloading $RC_SVCNAME"
	supervise-daemon "$RC_SVCNAME" --signal HUP
	eend $?
}
